package com.ketti.team;

import android.content.Context;
import android.graphics.Color;
import android.text.InputFilter;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class SourceView extends RelativeLayout {
	
	private NewEditText newSourceView;
	private LinearLayout searchBoxLayout;
	private Button searchBoxButton;
	private EditText searchBoxEdit;
	
	// This is keyword to highlight
	private final String[] keyWordList = new String[] {
			"abstract", "assert", "boolean", "break", "byte", 
			"case", "catch", "char", "class", "const",
			"continue", "default", "do", "double", "else", 
			"enum", "extends", "false", "final", "finally",
			"float", "for", "goto", "if", "implements",
            "import", "instanceof", "int", "interface", "long", 
            "native", "new", "null", "package", "private", "protected",
            "public", "return", "short", "static", "strictfp",
            "super", "switch", "synchronized", "this", "throw", "throws",
            "true", "transient", "try", "void", "volatile", "while"
            };
	
	private InputFilter keyWordInputFilter;

	public SourceView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public SourceView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public SourceView(Context context) {
		super(context);
		String inflaterService = Context.LAYOUT_INFLATER_SERVICE;
		LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(inflaterService);
		layoutInflater.inflate(R.layout.source_view, this, true);		
		
		newSourceView 	= (NewEditText) 	findViewById(R.id.source_editor);
		searchBoxLayout = (LinearLayout) 	findViewById(R.id.search_box_layout);
		searchBoxButton = (Button) 			findViewById(R.id.search_box_button);
		searchBoxEdit	= (EditText)		findViewById(R.id.search_box_edit);
		
		newSourceView.setSearchBoxLayout(searchBoxLayout);
		newSourceView.setSearchBoxEdit(searchBoxEdit);		
		newSourceView.setText(R.string.test_string);
		newSourceView.setTextSize(Setting.getmFontSize());
		newSourceView.setGravity(Gravity.TOP);			
		newSourceView.setHorizontallyScrolling(true);
		
		setInitailKeyWordFilter(newSourceView, keyWordList);						
		setKeyWordInputFilter();
		newSourceView.setFilters(new InputFilter[] {keyWordInputFilter});
		
		searchBoxButton.setTextSize(Setting.getmFontSize());
		searchBoxEdit.setTextSize(Setting.getmFontSize());
	}
	
	/**
	 * Setup keyword filter to highlight keyword in source. if user put 
	 * some keyword to EditText, this filter automatically change this word to red 
	 **/	
	 private void setKeyWordInputFilter() {
	    	keyWordInputFilter = new InputFilter() {
				
				@Override
				public CharSequence filter(CharSequence source, int start, int end,
						Spanned dest, int dstart, int dend) {
					for(String s : keyWordList)
					{
						if(s.equals(source))
						{						
							SpannableString sp = new SpannableString(source);
							sp.setSpan(new ForegroundColorSpan(Color.RED), 0, sp.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);						
							return sp;
						}
					}
					return source;
				}
			};
		}
	 
	/* Change keyword in initial source to red  */
	private void setInitailKeyWordFilter(NewEditText newSourceView,
			String[] keyWordList) {
		SpannableString source = new SpannableString(newSourceView.getText());
        
        for(String s : keyWordList){
        	if(source.toString().indexOf(s) != -1){
        		source.setSpan(new ForegroundColorSpan(Color.RED), source.toString().indexOf(s), source.toString().indexOf(s) + s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        		Log.d("match", Integer.toString(source.toString().indexOf(s)));
        	}
        }
        newSourceView.setText(source); 
	}
}
